//
//  FormalLoginViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/16.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FormalLoginViewController.h"
#import "FormalRegisterViewController.h"
#import "GlobalFunction.h"
#import "UserInfo.h"
#import "UserManger.h"

@interface FormalLoginViewController (){
    BOOL        phoneInputBool;
    BOOL        passwordInputBool;
}
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@end

@implementation FormalLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [((UIButton *)[self addLeftBtn:@"取消" withColor:nil]) addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [((UIButton *)[self addRigthBtn:@"注册" withColor:nil]) addTarget:self action:@selector(registerPress) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginBtn.enabled = NO;
    [self.loginBtn setBackgroundImage:getColorToImage(RGB(107, 118, 233)) forState:UIControlStateNormal];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)cancelPress{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)registerPress{
    FormalRegisterViewController *viewCon = [[FormalRegisterViewController alloc] initWithNibName:@"FormalRegisterViewController" bundle:nil];
    viewCon.RefreshHandle = ^(){
        self.RefreshHandle();
    };
    viewCon.RefreshPhone = ^(NSString *phone){
        self.phoneTextField.text = phone;
        phoneInputBool = YES;
    };
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)loginPress {
    [self.loginBtn setTitle:@"正在登录..." forState:UIControlStateNormal];
    self.loginBtn.userInteractionEnabled = NO;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.phoneTextField.text forKey:@"mobileNumber"];
    [dic setObject:self.passwordTextField.text forKey:@"password"];
    [[ServiceRequest sharedService] GETLogin:@"user/login" parameters:dic success:^(id responseObject) {
        [self.loginBtn setTitle:@"登录成功" forState:UIControlStateNormal];
        self.loginBtn.userInteractionEnabled = YES;
        NSDictionary *contentDic = [responseObject objectForKey:@"data"];
        NSString *phoneStr = [contentDic objectForKey:@"mobileNumber"];
        [DEFAULTS setObject:phoneStr forKey:USERID];
        [DEFAULTS synchronize];
        UserInfo *user = [UserManger currentLoggedInUser];
        if (!user) {
            user = [[UserInfo alloc] init];
        }
        user.userID = phoneStr;
        user.token = [contentDic objectForKey:@"accessToken"];
        user.phone = phoneStr;
        [UserManger setUser:user];
        
        self.RefreshHandle();
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(NSString *error, NSInteger code) {
        [self.loginBtn setTitle:@"重新登录" forState:UIControlStateNormal];
        self.loginBtn.userInteractionEnabled = YES;
        [self showHUDAlert:error];
    }];
}


#pragma mark---uitextfieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.phoneTextField) {
        if ([textField.text stringByReplacingCharactersInRange:range withString:string].length > 0) {
            phoneInputBool = YES;
        }else if([textField.text stringByReplacingCharactersInRange:range withString:string].length > 11){
            return NO;
        }else{
            phoneInputBool = NO;
        }
    }else if (textField == self.passwordTextField){
        if ([textField.text stringByReplacingCharactersInRange:range withString:string].length > 0) {
            passwordInputBool = YES;
        }else{
            passwordInputBool = NO;
        }
    }
    [self setLoginBtnClick];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    if (textField == self.phoneTextField) {
        phoneInputBool = NO;
    }else if (textField == self.passwordTextField){
        passwordInputBool = NO;
    }
    self.loginBtn.enabled = NO;
    return YES;
}

- (void)setLoginBtnClick{
    if (phoneInputBool && passwordInputBool) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}

- (IBAction)forgotPasswordPress {
    FormalRegisterViewController *viewCon = [[FormalRegisterViewController alloc] initWithNibName:@"FormalRegisterViewController" bundle:nil];
    viewCon.type = RegisterType_forgot;
    [self.navigationController pushViewController:viewCon animated:YES];
}
@end
